package com.mc.main;

import java.net.SocketException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Controller dummy
 * 
 * @author Mikael Bertlin
 *
 */
public class ControllerDummy extends Activity{

	private UnitAccessDummy uad;
	private UdpClient client;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cd_layout);

		client = UdpClient.client;
		uad = new UnitAccessDummy(client);
		client.addUdpEventListener(uad);
		
		final EditText et = (EditText) findViewById(R.id.EditTextNewSpeed);
		final TextView tv = (TextView) findViewById(R.id.TextViewSpeed);
		final Button button = (Button) findViewById(R.id.ButtonSetSpeed);
		
		tv.setText("Current Speed: " + Integer.toString(uad.getFan()));
		Log.v("onCreate: ", Integer.toString(uad.getFan()));
		
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.v("TAG", "click");
				String s = "";
				int val = 0;
				try {
					s = et.getText().toString();
					val = Integer.parseInt(s);
					Log.v("onClick: ", s);
				} catch (Exception e){}
				finally{
					et.setText("");
					if(val>255){
						Toast.makeText(getApplicationContext(), "Value to high", Toast.LENGTH_SHORT).show();
						return;
					}
					if(val<0){
						Toast.makeText(getApplicationContext(), "Value to low", Toast.LENGTH_SHORT).show();
						return;
					}
					uad.setFan(val);
					int fanSpeed = uad.getFan();
					tv.setText("Current Speed: " + Integer.toString(fanSpeed));
					Log.v("onClick: ", Integer.toString(fanSpeed));
				}
				
			}
		});
	}
}
